<?php
namespace Dingo\Api\Auth\Provider;

use Dingo\Api\Routing\Route;
use Illuminate\Http\Request;
use Illuminate\Auth\AuthManager;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

class Basic extends Authorization {
    protected $auth;
    protected $identifier;

    public function __construct(AuthManager $auth, $identifier = 'email') {
        $this->auth = $auth;
        $this->identifier = $identifier;
    }
    public function authenticate(Request $request, Route $route) {
        $this->validateAuthorizationHeader($request);

        if ( ($response = $this->auth->onceBasic($this->identifier)) && $response->getStatusCode() === 401 ) {
            throw new UnauthorizedHttpException('Basic', 'Invalid authentication credentials.');
        }

        return $this->auth->user();
    }

    public function getAuthorizationMethod() {
        return 'basic';
    }
}
